/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.javacommunity.controller.bean;

import com.javacommunity.model.dao.CategoryDAO;
import com.javacommunity.model.dao.CategoryTypeDAO;
import com.javacommunity.model.dao.impl.CategoryDAOImpl;
import com.javacommunity.model.dao.impl.CategoryTypeDAOImpl;
import com.javacommunity.model.entity.Category;
import com.javacommunity.model.entity.CategoryType;
import java.util.List;
import java.util.Set;
import javax.annotation.PostConstruct;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ManagedProperty;
import javax.faces.bean.RequestScoped;

/**
 *
 * @author Cong Pham Dinh
 */
@ManagedBean
@RequestScoped
public class CategoryBean {
    
    private final CategoryDAO categoryDAO = CategoryDAOImpl.getInstance();
    private final CategoryTypeDAO categoryTypeDAO = CategoryTypeDAOImpl.getInstance();
    
    private Category category;
    private List<Category> categories;
    private List<CategoryType> categoryTypes;
            
    
    @ManagedProperty(value="#{param.id}")
    private int categoryId;

    @PostConstruct
    public void init() {
        category = categoryDAO.getCategoryById(getCategoryId());
    }    
    
    public boolean createCategory(Category newCategory) {
        newCategory.setCategoryName(category.getCategoryName());
//        newCategory.setType(category.getType());
        newCategory.setActive(true);
        return categoryDAO.createCategory(newCategory);
    }

    /**
     * @return the category
     */
    public Category getCategory() {
        return category;
    }

    /**
     * @param category the category to set
     */
    public void setCategory(Category category) {
        this.category = category;
    }
    
    public List<Category> getCategories() {
        return categories;
    }

    public void setCategories(List<Category> categories) {
        this.categories = categories;
    }

    public List<CategoryType> getCategoryTypes() {
        categoryTypes = categoryTypeDAO.getCategoryTypes();
        return categoryTypes;
    }

    public void setCategoryTypes(List<CategoryType> categoryTypes) {
        this.categoryTypes = categoryTypes;
    }

    /**
     * @return the categoryId
     */
    public int getCategoryId() {
        return categoryId;
    }

    /**
     * @param categoryId the categoryId to set
     */
    public void setCategoryId(int categoryId) {
        this.categoryId = categoryId;
    }
    
}
